//
//  UIViewController+Navigation.m
//  VIVideoPlayer
//
//  Created by MN on 2022/6/28.
//

#import "UIViewController+Navigation.h"
#import <objc/runtime.h>
#import "EZCategory.h"

@implementation UIViewController (Navigation)
#pragma mark - set get
- (UIView *)vi_navView {
    return objc_getAssociatedObject(self, @selector(vi_navView));
}
- (void)setVi_navView:(UIView *)vi_navView {
    objc_setAssociatedObject(self, @selector(vi_navView), vi_navView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UILabel *)vi_titleLabel {
    return objc_getAssociatedObject(self, @selector(vi_titleLabel));
}
- (void)setVi_titleLabel:(UILabel *)vi_titleLabel {
    objc_setAssociatedObject(self, @selector(vi_titleLabel), vi_titleLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIButton *)vi_leftBtn{
    return objc_getAssociatedObject(self, @selector(vi_leftBtn));
}
- (void)setVi_leftBtn:(UIButton *)vi_leftBtn{
    objc_setAssociatedObject(self, @selector(vi_leftBtn), vi_leftBtn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIButton *)vi_rightBtn{
    return objc_getAssociatedObject(self, @selector(vi_rightBtn));
}
- (void)setVi_rightBtn:(UIButton *)vi_rightBtn {
    objc_setAssociatedObject(self, @selector(vi_rightBtn), vi_rightBtn, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void (^)(VIAction))vi_actionBlock{
    return objc_getAssociatedObject(self, @selector(vi_actionBlock));
}
- (void)setVi_actionBlock:(void (^)(VIAction))vi_actionBlock{
    objc_setAssociatedObject(self, @selector(vi_actionBlock), vi_actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(void))vi_rightActionBlock{
    return objc_getAssociatedObject(self, @selector(vi_rightActionBlock));
}
- (void)setVi_rightActionBlock:(void (^)(void))vi_rightActionBlock{
    objc_setAssociatedObject(self, @selector(vi_rightActionBlock), vi_rightActionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

#pragma mark - check

- (void)checkNavView{
    if (!self.vi_navView) {
        self.vi_navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavigationHeight)];
        self.vi_navView.backgroundColor = UIColor.whiteColor;
        [self.view addSubview:self.vi_navView];
    }
}
- (void)checkTitleLabel{
    if (!self.vi_titleLabel) {
        self.vi_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, kStatusBarSafeHeight, kScreenWidth-150, 44)];
        self.vi_titleLabel.textAlignment = NSTextAlignmentCenter;
        [self checkNavView];
        [self.vi_navView addSubview:self.vi_titleLabel];
    }
}

- (void)checkLeftBtn{
    if (!self.vi_leftBtn) { self.vi_leftBtn = [self addBtnWithX:0 action:@selector(leftAction)];}
}

- (void)checkRightBtn{
    if (!self.vi_rightBtn) {
        self.vi_rightBtn = [self addBtnWithX:kScreenWidth-50 action:@selector(rightAction)];
        uint32_t hex = 0x333333;
        UIColor *color = [UIColor colorWithRed:((hex & 0xFF0000) >> 16) / 255.0f
                                         green:((hex & 0xFF00) >> 8) / 255.0f
                                          blue:(hex & 0xFF) / 255.0f
                                         alpha:1];
        [self.vi_rightBtn setTitleColor:color forState:UIControlStateNormal];
        self.vi_rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    }
}

/// 创建按钮 添加
/// @param x 按钮的x坐标
/// @param action 响应事件
- (UIButton *)addBtnWithX:(CGFloat)x action:(SEL)action{
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(x, kStatusBarSafeHeight, 50, 44);
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self checkNavView];
    [self.vi_navView addSubview:btn];
    return btn;
}
#pragma mark - Actions

/// 左边按钮事件
- (void)leftAction{
    [self action:VIActionLeft];
}

/// 右边按钮事件
- (void)rightAction{
    [self action:VIActionRight];
    if (self.vi_rightActionBlock) self.vi_rightActionBlock();
}
- (void)action:(VIAction)actionType{
    if (self.vi_actionBlock) {
        self.vi_actionBlock(actionType);
    }else if (actionType == VIActionLeft){
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - public
/// 更新导航标题
/// @param title 标题
- (void)vi_updateTitle:(NSString *)title{
    [self checkTitleLabel];
    self.vi_titleLabel.text = title;
}

/// 设置导航右边的文字
/// @param title 文字
- (void)vi_setRightWithTitle:(NSString *)title {
    [self checkRightBtn];
    [self.vi_rightBtn setTitle:title forState:UIControlStateNormal];
}

/// 设置导航左边的图片
/// @param image 图片
- (void)vi_setLeftWithImage:(UIImage *)image{
    [self checkLeftBtn];
    [self.vi_leftBtn setImage:image forState:UIControlStateNormal];
}

/// 设置导航右边的图片
/// @param image 图片
- (void)vi_setRightWithImage:(UIImage *)image{
    [self checkRightBtn];
    [self.vi_rightBtn setImage:image forState:UIControlStateNormal];
}

/// 更新导航样式
- (void)vi_updateBackgroundColor:(UIColor *)backgroundColor
                       textColor:(UIColor *)textColor
                       leftImage:(UIImage *)leftImage
                      rightImage:(UIImage *)rightImage{
    [self checkTitleLabel];
    self.vi_navView.backgroundColor = backgroundColor;
    self.vi_titleLabel.textColor = textColor;
    [self vi_setLeftWithImage:leftImage];
    [self vi_setRightWithImage:rightImage];
}

- (BOOL)vi_backControllerWithName:(NSString *)name{
    UIViewController *target = nil;
    for (UIViewController * controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:NSClassFromString(name)]) {
            target = controller;
        }
    }
    if (target) [self.navigationController popToViewController:target animated:YES];
    return target ? YES : NO;
}

- (void)vi_backControllerOrRootWithName:(NSString *)name{
    BOOL relust = [self vi_backControllerWithName:name];
    if(!relust){
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
@end
